package com.lmc.util;


import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateFormatUtil {

    /**
     * 把毫秒值转化为yyyy-MM-dd
     *
     * @param currentTimeMillis
     * @return
     */
    public static String forString(long currentTimeMillis) {
        return forString(currentTimeMillis, null);
    }

    /**
     * 把毫秒值转化为format格式的日期
     *
     * @param currentTimeMillis
     * @return
     */
    public static String forString(long currentTimeMillis, String format) {
        Date d = new Date(currentTimeMillis);
        if ((format == null) || format.equals("")) {
            format = "yyyy-MM-dd";
        }
        DateFormat df = new SimpleDateFormat(format);
        String str = df.format(d);
        return str;
    }

    /**
     * 取得当前时间戳（精确到毫秒）
     */
    public static long getTime() {
        long time = System.currentTimeMillis();
        return time;
    }

    /**
     * 毫秒转为时/分/秒
     */
    public static String toHMS(long currentTimeMillis) {
        long ls = currentTimeMillis / 1000;
        String s = (ls % 60) + "秒";
        int im = (int) (ls / 60);
        String m = "";
        if (im > 0) {
            m = (im % 60) + "分钟";
        }
        String h = "";
        int ih = im / 60;
        if (ih > 0) {
            h = ih + "小时";
        }
        return h + m + s;
    }

    public static String getPlayTime(long time) {
        String s = (time % 60) + "秒";
        int im = (int) (time / 60);
        String m = "";
        if (im > 0) {
            m = (im % 60) + "分钟";
        }
        String h = "";
        int ih = im / 60;
        if (ih > 0) {
            h = ih + "小时";
        }
        return h + m + s;
    }

    /**
     * 把日期格式化的时间转化为《微信朋友圈》格式显示
     *
     * @param time:时间戳
     * @return
     */
    public static String getString(long time) {
        return getString(forString(time));
    }

    public static String getString(String rel_time) {
        String backStr = "刚才";
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //当前的时间
        Date curDate = new Date(System.currentTimeMillis());
        String now_time = format.format(curDate);
        Date d1 = null;
        Date d2 = null;
        try {
            d1 = format.parse(rel_time);
            d2 = format.parse(now_time);
            // 毫秒ms
            long diff = d2.getTime() - d1.getTime();
            long diffMinutes = diff / (60 * 1000) % 60;
            long diffHours = diff / (60 * 60 * 1000) % 24;
            long diffDays = diff / (24 * 60 * 60 * 1000);
            if (diffDays != 0) {
                if (diffDays < 30) {
                    if (1 < diffDays && diffDays < 2) {
                        backStr = "昨天";
                    } else if (1 < diffDays && diffDays < 2) {
                        backStr = "前天";

                    } else {

                        backStr = String.valueOf(diffDays) + "天前";
                    }
                } else {
                    backStr = "很久以前";
                }

            } else if (diffHours != 0) {
                backStr = String.valueOf(diffHours) + "小时前";

            } else if (diffMinutes != 0) {
                backStr = String.valueOf(diffMinutes) + "分钟前";

            } else {

                backStr = "刚刚";

            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        return backStr;

    }

    public static String getMessageDate(String dateOriginal) {
        long original = 0;
        long oneDay = 1000 * 60 * 60 * 24;
        try {
            original = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(dateOriginal).getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        //当前的时间
        Calendar today = Calendar.getInstance();
        today.set(Calendar.HOUR_OF_DAY, 0);
        today.set(Calendar.MINUTE, 0);
        today.set(Calendar.SECOND, 0);
        today.set(Calendar.MILLISECOND, 0);

        if (original >= today.getTimeInMillis()) {
            // 今天
            return dateOriginal.substring(11, 16);
        } else if (original >= today.getTimeInMillis() - oneDay) {
            // 昨天
            return "昨天" + dateOriginal.substring(11, 16);
        } else {
            return dateOriginal.substring(0, 16);
        }
    }
}
